<?php

// +----------------------------------------------------------------------
// | Shop-Demo for ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2022~2023 Anyon <zoujingli@qq.com>
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 免责声明 ( https://thinkadmin.top/disclaimer )
// | 会员免费 ( https://thinkadmin.top/vip-introduce )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\data\controller\daikuan;

use app\data\model\Loans as DataNewsItem;
use app\data\service\NewsService;
use app\data\service\NoticeService;
use think\admin\Controller;
use think\admin\extend\CodeExtend;
use think\admin\helper\QueryHelper;
use think\facade\Db;

/**
 * 资金管理
 * Class Item
 * @package app\data\controller\news
 */
class Loans extends Controller
{
    /**
     * 资金管理
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $this->type = $this->get['type'] ?? 'index';
        DataNewsItem::mQuery($this->get)->layTable(function (){
            $this->title = '资金管理';
        }, function (QueryHelper $query){
            $query->like('loans_name')->dateBetween('create_at');
            $query->where(['status' => intval($this->type === 'index'), 'deleted' => 0]);
        });
    }

    /**
     * 列表数据处理
     * @param  array  $data
     */
    protected function _page_filter(array &$data)
    {
        foreach ($data as &$item) {

            if (!empty($item['institution_type'])) {
                $item['institution_type'] = Db::name("loans_institution_types")->where('id', $item['institution_type'])->value('institutions_type_name');
            } else {
                $item['institution_type'] = '';
            }

            if (!empty($item['method'])) {

                $method_ids = explode(',',$item['method']);
                $methods = Db::name('loans_method')
                    ->where('id', 'in', $method_ids) // 使用in查询匹配多个ID
                        ->where('deleted',0)
                    ->column('method_name'); // 假设你要查询的是用户的name字段，并且只获取这个字段的值
                $item['method'] = implode(',',$methods);
            } else {
                $item['method'] = '';
            }

            if (!empty($item['repayment'])) {
                $item['repayment'] = Db::name("loans_repayment")->where('id', $item['repayment'])->value('repayment_name');
            } else {
                $item['repayment'] = '';
            }

            if (!empty($item['institution'])) {
                $item['institution'] = Db::name("loans_institutions")->where('id', $item['institution'])->value('institutions_name');
            } else {
                $item['institution'] = '';
            }


            if (!empty($item['services_process'])) {

                $services_process_ids = explode(',',$item['services_process']);
                $services_processs = Db::name('loans_service_process')
                    ->where('id', 'in', $services_process_ids) // 使用in查询匹配多个ID
                    ->column('process_name'); // 假设你要查询的是用户的name字段，并且只获取这个字段的值
                $item['services_process'] = implode(',',$services_processs);
            } else {
                $item['services_process'] = '';
            }

        }

    }

    /**
     * 添加文章内容
     */
    public function add()
    {
        $this->title = '添加内容';
        DataNewsItem::mForm('form');
    }

    /**
     * 编辑文章内容
     */
    public function edit()
    {
        $this->title = '编辑内容';
        DataNewsItem::mForm('form');
    }

    /**
     * 表单数据处理
     * @param  array  $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _form_filter(array &$data)
    {
        if ($this->request->isGet()) {

            $this->institutions_type_list = Db::name("loans_institution_types")->where('deleted',0)->select()->toArray();
            $this->repayment_list = Db::name("loans_repayment")->where('deleted',0)->select()->toArray();
            $this->institution_list = Db::name("loans_institutions")->where('deleted',0)->select()->toArray();


        } else {

        }
    }

    public function getMethodList()
    {
        $gongzuo = Db::name("loans_method")->where('deleted',0)->select()->toArray();
        return_msg(200,'success',$gongzuo);
    }

    public function getServicesProcessList()
    {
        $gongzuo = Db::name("loans_service_process")->where('deleted',0)->select()->toArray();
        return_msg(200,'success',$gongzuo);
    }


    /**
     * 表单结果处理
     * @param  boolean  $state
     */
    protected function _form_result(bool $state, $data)
    {
        if ($state) {
            $this->success('保存成功！', 'javascript:history.back()');
        }
    }

    /**
     * 修改文章状态
     */
    public function state()
    {
        DataNewsItem::mSave($this->_vali([
            'status.in:0,1'  => '状态值范围异常！',
            'status.require' => '状态值不能为空！',
        ]));
    }

    /**
     * 删除文章内容
     */
    public function remove()
    {
        DataNewsItem::mDelete();
    }

    /**
     * 文章内容选择
     * @login true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function select()
    {
        $this->get['status'] = 1;
        $this->index();
    }

}